package com.leoman.controller.weixin;

import com.leoman.controller.IndexController;
import com.leoman.core.Configue;
import com.leoman.entity.Area;
import com.leoman.entity.Image;
import com.leoman.entity.Question;
import com.leoman.entity.Type;
import com.leoman.entity.vo.WxContactVo;
import com.leoman.entity.vo.WxUserVo;
import com.leoman.service.AreaService;
import com.leoman.service.ImageService;
import com.leoman.service.QuestionService;
import com.leoman.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 微信控制层
 * Created by 涂奕恒 on 2016/3/16.
 */
@RequestMapping(value = "weixin/info")
@Controller
public class WeixinBaseController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private ImageService imageService;

    @Autowired
    private AreaService areaService;

    @RequestMapping("/error")
    public String error() {
        return "error/errorPlus";
    }

    @RequestMapping("/")
    public String base() {
        return "weixin/feedback";
    }

    @RequestMapping("/typeList")
    @ResponseBody
    public List<Type> typeList() {
        return typeService.findList();
    }

    @RequestMapping("/areaList")
    @ResponseBody
    public List<Area> areaList() {
        return areaService.findParentList();
    }

    @RequestMapping("/areaListPlus")
    @ResponseBody
    public List<Area> areaListPlus(Integer areaId) {
        return areaService.findList(areaId);
    }

    @RequestMapping("/sectionList")
    @ResponseBody
    public List<WxContactVo> sectionList(HttpServletRequest request) {
        return getWxContactList(request);
    }

    @RequestMapping("/memberList")
    @ResponseBody
    public List<WxUserVo> memberList(HttpServletRequest request, Integer contactId) {
        try {
            for (WxContactVo wxContactVo : getWxContactList(request)) {
                if (wxContactVo.getId().equals(contactId)) {
                    return wxContactVo.getUserList();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @RequestMapping("/addInfo")
    public String addInfo(HttpServletRequest request, ModelMap modelMap) {
        try {
            // 获取问题类型列表
            List<Type> typeList = typeService.findList();
            modelMap.addAttribute("wxContactList", getWxContactList(request));
            modelMap.addAttribute("typeList", typeList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "weixin/feedback";
    }

    @RequestMapping("/contactList")
    public String contactList(HttpServletRequest request, ModelMap modelMap) {
        try {
            modelMap.addAttribute("wxContactVoList", getWxContactList(request));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "weixin/contacts";
    }

    @RequestMapping("/contactDetail")
    public String contactDetail(HttpServletRequest request, Integer id, ModelMap modelMap) {
        for (WxContactVo wxContactVo : getWxContactList(request)) {
            if (wxContactVo.getId().equals(id)) {
                modelMap.addAttribute("contactDetail", wxContactVo);
            }
        }

        return "weixin/contacts-detail";
    }

    @RequestMapping("/questionInfo")
    public String questionInfo(Integer id, ModelMap modelMap) {
        Question question = questionService.getById(id);

        List<Image> imageList = imageService.findListByQuestionId(id);

        modelMap.addAttribute("question", question);
        modelMap.addAttribute("imageList", imageList);

        return "weixin/feedback-detail";
    }

    @RequestMapping("/addQuestion")
    @ResponseBody
    public Integer addQuestion(HttpServletRequest request,
                               Integer areaId,
                               Integer typeId,
                               String memberName,
                               String contactText,
                               String description,
                               Integer userType) {
        return questionService.addQuestion(request, areaId, typeId, memberName, contactText, description, userType);
    }

    @RequestMapping("/addQuestionImage")
    @ResponseBody
    public Integer addQuestionImage(MultipartRequest multipartRequest, Integer questionId) {
        return imageService.batchAddImage(multipartRequest, questionId);
    }

    /**
     * 获取部门信息
     */
    @SuppressWarnings("unchecked")
    public static List<WxContactVo> getWxContactList(HttpServletRequest request) {
        return IndexController.getContactList(request);
    }
}
